﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Framework.IModel;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Model;

namespace FantastPlatform.Community.UI.Service
{
    public partial class PersonRoleService : BasePage
    {
        protected int RoleId
        {
            get
            {
                int roleId = 0;
                if (!int.TryParse(Request["RoleId"], out roleId))
                {
                    roleId = 0;
                }
                return roleId;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IRoleService roleService = ServiceFactory.CreateService<IRoleService>();
            IUserService userService = ServiceFactory.CreateService<IUserService>();
            IRole roles = new FantastPlatform.Framework.Model.Role();
            IUser users = new FantastPlatform.Framework.Model.User();
            IList<IUser> userList;
            ConditionGroup group = new ConditionGroup();
            Condition enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
            group.Conditions.Add(enableCondition);
            int totalCount = 0;
            switch (Action)
            {
                case "SearchRoles":
                    group.Conditions.Add(new Condition { Field = "Name", Type = typeof(string).FullName, Relation = Relations.And, Value1 = "社区%", Comparison = Comparisons.Like });
                    IList<IRole> roleList = roleService.Search(group,null, int.MaxValue, 1, out totalCount);
                    object[] roleObj = (from o in roleList
                                        select new
                                        {
                                            Id = o.Id,
                                            Code = o.Code,
                                            Name = o.Name
                                        }).ToArray();
                    ReturnList(totalCount, roleObj);
                    break;
                case "Search":
                    userList = userService.LoadList(null, PageSize, SelfPage, out totalCount);
                    object objUserrole = (from o in userList
                                          select new
                                          {
                                              Id = o.Id,
                                              Code = o.Code,
                                              Name = o.Name
                                          }
                                    ).ToArray();
                    ReturnList(totalCount, objUserrole);
                    break;
                case "SearchUser":
                    group.Conditions.Add(new Condition { Field = "Employees.EmployeePosts.Post.Organization.Name", Relation = Relations.And, Type = typeof(string).FullName, Comparison = Comparisons.Like, Value1 = "%派出所" });

                    userList = userService.Search(group, null, PageSize, SelfPage, out totalCount);
                    object objArr = (from o in userList
                                     select new
                                     {
                                         Id = o.Id,
                                         Code = o.Code,
                                         Name = o.Name
                                     }
                                    ).ToArray();
                    ReturnList(totalCount, objArr);
                    break;
                case "RemoveUser":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的选中用户Id为空");
                    }
                    List<int> idruList = new List<int>();
                    string[] idruStrList = Data.Split(',');
                    foreach (string id in idruStrList)
                    {
                        idruList.Add(int.Parse(id));
                    }
                    IRole role = roleService.Get(RoleId);
                    if (role != null)
                    {
                        roleService.RemoveUsers(role.Id, idruList);
                    }
                    ReturnSuccessMessage("移除成功");
                    break;
                case "AddUser":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的选中用户Id为空");
                    }
                    List<int> idUserList = new List<int>();
                    string[] idUserStrList = Data.Split(',');

                    foreach (string id in idUserStrList)
                    {
                        idUserList.Add(int.Parse(id));
                    }
                    role = roleService.Get(RoleId);
                    roleService.AddUsers(role.Id, idUserList);
                    ReturnSuccessMessage("添加成功");
                    break;
                case "SearchAddUser":
                    role = roleService.Get(RoleId);
                    object[] objs = (from ru in role.Users
                                     select new
                                     {
                                         Id = ru.Id,
                                         Name = ru.Name,
                                         Code = ru.Code,
                                         Description = ru.Description
                                     }).Skip((SelfPage - 1) * PageSize).Take(PageSize).ToArray();
                    ReturnList(role.Users.Count, objs);
                    break;
                case "GetPolice":
                    int totalpolice = 0;
                    group.Conditions.Add(new Condition { Field = "Employees.EmployeePosts.Post.Organization.Name", Relation = Relations.And, Type = typeof(string).FullName, Comparison = Comparisons.Like, Value1 = "%派出所" });
                    if (!string.IsNullOrEmpty(Request["name"]))
                    {
                        Condition nameCodidton = new Condition() { Field = "Name", Type = typeof(string).FullName, Value1 = Request["Name"], Comparison = Comparisons.Like, Relation = Relations.And };
                        group.Conditions.Add(nameCodidton);
                    }
                    if (!string.IsNullOrEmpty(Request["code"]))
                    {
                        Condition codeCodidton = new Condition() { Field = "Code", Type = typeof(string).FullName, Value1 = Request["Code"], Comparison = Comparisons.Like, Relation = Relations.And };
                        group.Conditions.Add(codeCodidton);
                    }
                    IList<IUser> policelist = userService.Search(group, null, PageSize, 1, out totalpolice);

                    object[] objpolice = (from o in policelist
                                          select new
                                          {
                                              Id = o.Id,
                                              Code = o.Code,
                                              Name = o.Name
                                          }
                                     ).ToArray();
                    ReturnList(totalpolice, objpolice);
                    break;
            }
        }
    }
}